package com.wf.demo.info.service;

import com.wf.demo.info.dao.CompanyDAO;
import com.wf.demo.info.dao.DeviceDAO;
import com.wf.demo.info.dao.ModelDAO;
import com.wf.demo.info.dao.ParseDAO;
import com.wf.demo.info.pojo.*;
import com.wf.demo.info.util.Page4Navigator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;


@Service
public class ModelService {
    @Autowired
    ModelDAO modelDAO;
    @Autowired
    ParseService parseService;
    @Autowired
    MatchService matchService;
    @Autowired
    CompanyDAO companyDAO;
    @Autowired
    DeviceDAO deviceDAO;
    public List<Model> list(){
        Sort sort = new Sort(Sort.Direction.ASC,"id");
        return modelDAO.findAll(sort);
    }
    public Page4Navigator<Model> list(int start, int size, int navigatePages) {
        Sort sort = new Sort(Sort.Direction.ASC, "id");
        Pageable pageable = new PageRequest(start, size, sort);
        Page pageFromJPA = modelDAO.findAll(pageable);
        return new Page4Navigator<>(pageFromJPA, navigatePages);
    }
    public void delete(int id){
        modelDAO.delete(id);
    }
    public void update(Model bean){
        modelDAO.save(bean);
    }
    public void add(Model bean){
        modelDAO.save(bean);
    }
    public int isExist(String name,String number) {
       Model model1 = modelDAO.findByModelName(name);
       Model mode2=modelDAO.findByModelNumber(number);
       if (null != model1){
           return 1;
       }
       if (null != mode2){
           return 2;
       }
       return  0;
    }
    public int isExist(Model bean) {
        int id =bean.getId();
        Model model =modelDAO.getOne(id);

        String name =bean.getModelName();
        String number=bean.getModelNumber();
        Model model1 = modelDAO.findByModelName(name);
        Model mode2=modelDAO.findByModelNumber(number);
        if (null != model1 && !model.getModelName().equals(bean.getModelName())){
            return 1;
        }
        if (null != mode2  && !model.getModelNumber().equals(bean.getModelNumber())){
            return 2;
        }
        return  0;
    }
    public void modelInit(){
        //得到刚刚插入的机型机型初始化配置
        Model model=modelDAO.findFirstByOrderByIdDesc();
        parseService.addModelInit(model);
        matchService.addStatusParseInit(model);
        matchService.addAlarmCodeParseInit(model);
    }
    public Model get(int id){
      return  modelDAO.findOne(id);
    }

    /**
     *功能描述  某型号设备总数xx，A厂有该型号xx，B厂有型号xx，C厂有型号xx
     *
     * @author wf
     * @date 2019/12/23
     * @param
     * @return java.lang.Object
     */
    public  Object statisticDeviceAndModel(){
        List<Company> companies=companyDAO.findAll();
        List<Model> models =modelDAO.findAll();
        LinkedHashMap<String, Object> map=new LinkedHashMap<>();
        //每个机型设备总数量
        long totalNumber =0;
        //每个公司某种机型设备数量
        long number=0;

        for(Model model: models){
            //用来储存每种机型的信息
            LinkedHashMap<String ,Object> obj =new LinkedHashMap<>();
            //统计某机型所有的数量
            totalNumber = deviceDAO.countByModel(model);
            obj.put("totalNumber",totalNumber);
            //统计该公司下每种设备的机型数量
            for (Company company:companies){
                number=deviceDAO.countByCompanyAndModel(company,model);
                //如果数量不为0
                if (number !=0)
                    obj.put(company.getName(),number);
            }
            map.put(model.getModelName(),obj);
        }
        return  map;
    }


}

